package com.bawei.module_home.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_home.R
import com.bawei.module_home.databinding.ItemMerchantBinding
import com.bawei.module_home.http.HomeServiceViewModel
import com.bawei.module_home.model.MerchantModelItem

class MerchantAdapter: RecyclerView.Adapter<MerchantAdapter.MerchantViewHolder>() {
    val list=mutableListOf<MerchantModelItem>()

    interface OnItemClickListener{
        fun clickListener(item: MerchantModelItem, position: Int)
    }
    var itemClickLister: OnItemClickListener? = null

    fun setOnItemClickListener(listener: OnItemClickListener){
        itemClickLister=listener
    }


    fun setData(data:List<MerchantModelItem>){
        list.clear()
        list.addAll(data)
        notifyDataSetChanged()
    }
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): MerchantViewHolder {
        val binding = ItemMerchantBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
        return MerchantViewHolder(binding)
    }

    override fun onBindViewHolder(
        holder: MerchantViewHolder,
        position: Int
    ) {
        val binding=DataBindingUtil.getBinding<ItemMerchantBinding>(holder.itemView) ?: return
        binding.merchantNumber.text="申请编号"+list[position].applicationNumber
        binding.merchantTime.text="申请时间"+list[position].entryTime
        if(list[position].status.equals("新建")){
            binding.merchantStatus.background=ContextCompat.getDrawable(binding.root.context,R.drawable.area_status_green)
        }else{
            binding.merchantStatus.text=list[position].status
            binding.merchantStatus.background=ContextCompat.getDrawable(binding.root.context, R.drawable.area_status_blue)
        }
        binding.merchantStatus.setOnClickListener({

        })

        holder.itemView.setOnClickListener{
            itemClickLister?.clickListener(list[position],position)
        }


    }

    override fun getItemCount(): Int {
        return list.size
    }

    class MerchantViewHolder(val binding: ItemMerchantBinding) : RecyclerView.ViewHolder(binding.root)
}